﻿Imports Negocio
Public Class frmBusqCliente

    Dim servicio1 As New ClienteNegocio
    Dim servicio2 As New PedidoNegocio

    Dim empezar As Boolean = False

    Sub listarDistritos()
        With cboDistrito
            .DataSource = servicio2.listarDistritosXProvincia(cboProvincia.SelectedValue.ToString).Tables(0)
            .ValueMember = "DIS_Codigo"
            .DisplayMember = "DIS_Descripcion"
        End With
    End Sub

    Sub listarProvincias()
        With cboProvincia
            .DataSource = servicio2.listarProvinciaXDepartamento(cboDepartamento.SelectedValue.ToString).Tables(0)
            .ValueMember = "PRO_Codigo"
            .DisplayMember = "PRO_Descripcion"
        End With
    End Sub

    Sub listarDepartamentos()
        With cboDepartamento
            .DataSource = servicio2.listarDepartamentos().Tables(0)
            .ValueMember = "DEP_Codigo"
            .DisplayMember = "DEP_Descripcion"
        End With
    End Sub

    Sub inicializar()
        listarDepartamentos()
        listarProvincias()
        listarDistritos()
        empezar = True
    End Sub

    Sub buscarCliente()
        Dim ds As DataSet = servicio1.obtenerClientesXFiltro(txtCodigo.Text.Trim, txtRuc.Text.Trim, txtDescripcion.Text.Trim,
                                                             txtVendedor.Text.Trim, cboDistrito.SelectedValue.ToString,
                                                             cboProvincia.SelectedValue.ToString, cboDepartamento.SelectedValue.ToString)

        dgvCliente.DataSource = ds.Tables(0)

        If ds.Tables(0).Rows.Count Then
            dgvCliente.Rows(0).Selected = False
        End If
    End Sub

    Private Sub frmBusqCliente_Load(ByVal sender As Object, ByVal e As System.EventArgs) Handles Me.Load
        inicializar()
    End Sub

    Private Sub cboDepartamento_SelectedValueChanged(sender As Object, e As System.EventArgs) Handles cboDepartamento.SelectedValueChanged
        If empezar = True Then
            listarProvincias()
        End If
    End Sub

    Private Sub cboProvincia_SelectedValueChanged(sender As Object, e As System.EventArgs) Handles cboProvincia.SelectedValueChanged
        If empezar = True Then
            listarDistritos()
        End If
    End Sub

    Private Sub btnBuscar_Click(sender As System.Object, e As System.EventArgs) Handles btnBuscar.Click
        buscarCliente()
    End Sub

    Private Sub txtCodigo_KeyPress(sender As Object, e As System.Windows.Forms.KeyPressEventArgs) Handles txtCodigo.KeyPress
        If e.KeyChar = ChrW(Keys.Escape) Then
            Me.Close()
        End If
    End Sub

    Private Sub txtVendedor_KeyDown(sender As Object, e As System.Windows.Forms.KeyEventArgs) Handles txtVendedor.KeyDown
        Dim texto As String = txtVendedor.Text.Trim

        If e.KeyValue = Keys.Enter Then
            If texto = "" Then
                lblVendedor.Text = ""
            Else
                Dim ds As DataSet = servicio2.obtenerListaVendedores(texto)
                If ds.Tables(0).Rows.Count <> 0 Then
                    lblVendedor.Text = ds.Tables(0).Rows(0)("VEN_Descripcion").ToString.Trim
                    'focus ??
                End If
            End If
        ElseIf e.KeyValue = Keys.Add Then
            Dim ds As DataSet = servicio2.obtenerListaVendedores("")
            Dim vende As New frmVendedor(ds)
            If vende.ShowDialog() = Windows.Forms.DialogResult.OK Then
                'focuis???
            End If
        End If
    End Sub

    Private Sub dgvCliente_CellClick(sender As Object, e As System.Windows.Forms.DataGridViewCellEventArgs) Handles dgvCliente.CellClick
        Dim f As Integer = e.RowIndex

        If f > -1 Then
            'frmPedido.txtCliente.Text = dgvCliente.Item("clmCodigo", f).Value.ToString.Trim
            frmMenu.formDocumento.txtCliente.Text = dgvCliente.Item("clmCodigo", f).Value.ToString.Trim
            DialogResult = Windows.Forms.DialogResult.OK
        End If
    End Sub

    Private Sub dgvCliente_KeyDown(sender As Object, e As System.Windows.Forms.KeyEventArgs) Handles dgvCliente.KeyDown
        If e.KeyValue = Keys.Enter Then
            frmMenu.formDocumento.txtCliente.Text = dgvCliente.Rows(dgvCliente.CurrentRow.Index).Cells("clmCodigo").Value
            DialogResult = Windows.Forms.DialogResult.OK
        End If
    End Sub

End Class